package fun._6742.controller;


import fun._6742.constant.RedisCacheKey;
import fun._6742.model.valueobject.UserVO;
import fun._6742.request.LoginRequest;
import fun._6742.request.RechargeRequest;
import fun._6742.request.RegisterRequest;
import fun._6742.request.UpdateUserInfoRequest;
import fun._6742.service.FileService;
import fun._6742.service.PointChangeService;
import fun._6742.service.UserService;
import fun._6742.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiuQ
 * @since 2022-09-18
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {

    // region Resources
    @Resource
    FileService fileService;
    @Resource
    UserService userService;
    @Resource
    PointChangeService pointChangeService;

    // endregion

    /**
     * 用户注册：使用图形验证码验证
     *
     * @param registerRequest
     * @param request
     */
    @ApiOperation(value = "注册", notes = "使用图形验证码验证")
    @PostMapping("register")
    public void register(@ApiParam(value = "用户注册信息", required = true)
                         @RequestBody RegisterRequest registerRequest,
                         HttpServletRequest request) {
        String captchaKey = produceCaptchaKey(request);
        userService.userRegister(captchaKey, registerRequest);
    }

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public String login(@ApiParam(value = "用户登录信息", required = true)
                        @RequestBody LoginRequest loginRequest,
                        HttpServletRequest request) {
        return userService.userLogin(loginRequest, request);
    }

    @ApiOperation(value = "手机号唯一性检查")
    @PostMapping("phoneUnique")
    public void checkPhoneUnique(@ApiParam(value = "手机号", required = true)
                                 @RequestBody String phone) {
        userService.checkPhoneUnique(phone);
    }

    @ApiOperation("用户详情")
    @GetMapping("detail")
    public UserVO detail() {
        return userService.findUserDetail();
    }


    @ApiOperation("用户信息更新")
    @PostMapping("update")
    public void update(@ApiParam(value = "用户修改信息", required = true)
                       @RequestBody UpdateUserInfoRequest updateUserInfoRequest) {
        userService.updateUserDetail(updateUserInfoRequest);
    }

    /**
     * 上传用户头像，大小0.64-10M，超过会报错
     *
     * @param url 文件
     */
    @ApiOperation("头像上传")
    @PostMapping(value = "upload")
    public void uploadHeaderImg(@ApiParam(value = "图片url", required = true) @RequestBody String url) {
        fileService.uploadUserHeadImg(url);
    }

    /**
     *
     */
    @ApiOperation("充值")
    @PostMapping(value = "recharge")
    public void addPoints(@ApiParam(value = "金额", required = true) @RequestBody RechargeRequest rechargeRequest) {
        userService.addPoints(rechargeRequest.getAmount());
        pointChangeService.add(rechargeRequest.getAmount());
    }

    @ApiOperation("获取积分记录")
    @GetMapping(value = "records")
    public Object addPoints(@ApiParam(value = "id") @RequestParam(value = "id", defaultValue = "-1") long id,
                            @ApiParam(value = "当前页") @RequestParam(value = "page", defaultValue = "1") int page,
                            @ApiParam(value = "每页显示条数") @RequestParam(value = "size", defaultValue = "10") int size) {
        if (id > 0) {
            return pointChangeService.findOne(id);
        } else {
            return pointChangeService.findPage(page, size);
        }
    }

    /**
     * 获取验证码缓存的key
     *
     * @param request
     * @return
     */
    public static String produceCaptchaKey(HttpServletRequest request) {
        String ip = request.getRemoteAddr();
        String userAgent = request.getHeader("User-Agent");
        return String.format(RedisCacheKey.CHECK_CODE_KEY, CommonUtils.MD5(ip + userAgent));
    }

}

